package com.openatc.agent.config.arterial;

import com.openatc.agent.service.arterial.ArterialTrafficControl;
import com.openatc.agent.service.arterial.ArterialTrafficControlExecutor;
import com.openatc.agent.service.arterial.exception.UnsupportedArterialTrafficControlException;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author ：panleilei
 * @description
 * @modified By：
 * @version:
 * @date ：Created in 2024/1/23 14:34
 */
@Configuration
public class ArterialTrafficControlConfig {
    /**
     * 容器启动时，向容器中注入一个策略执行器，需要执行ArterialTrafficControl策略时，直接从容器中获取策略执行器即可
     * @param strategyList
     * @return
     */
    @Bean
    public ArterialTrafficControlExecutor arterialTrafficControlExecutor(List<ArterialTrafficControl> strategyList) {
        Map<String, ArterialTrafficControl> strategies = strategyList.stream().collect(Collectors.toMap(ArterialTrafficControl::strategy, s -> s));
        return routePara -> {
            ArterialTrafficControl arterialTrafficControl = strategies.get(routePara.getOptstrategy());
            if (arterialTrafficControl == null) {
                throw new UnsupportedArterialTrafficControlException(routePara.getOptstrategy());
            }
            return arterialTrafficControl.optimize(routePara);
        };
    }
}
